package com.xiaoYuan;

import javax.swing.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {

    // 创建一个集合对象用来存储密码集合
    private static LinkedList<PassWord> linkedList = new LinkedList<PassWord>();

    public static void main(String[] args) {

        // 创建输入对象
        Scanner s = new Scanner(System.in);
        String sign;

        // 创建所需要的目录
        File file = new File("user\\pass");
        if(!file.exists()){
            file.mkdirs();
        }

        // 没有密码创建密码，有密码输入密码并且判断
        String logPassWord;
        String appPassWord = PassWordToll.getPassWord( "user\\pass\\pass.mm", s);
        if (!"".equals(appPassWord)){
            while (true){
                // 每次输入密码之前更新appPassWord中的密码
                appPassWord = PassWordToll.getPassWord( "user\\pass\\pass.mm", s);
                System.out.print("请输入登录密码：");
                logPassWord = s.next();
                if(logPassWord.equals(appPassWord)){
                    System.out.println("登录成功！");
                    break;
                }else {
                    System.out.println("密码错误，登陆失败！");
                }
            }
        }

        // 加载本地密码集合
        linkedList = PassWordToll.passWordObjectLoad("user\\passWord.jpg", linkedList);

        while (true){
            // 加载页面
            PassWordToll.page();
            System.out.print("请输入：");
            sign = s.next();
            if ("1".equals(sign)) {
                // 查询密码 ok
                System.out.print("输入查询的内容：");
                String lookPassWord = s.next();

                // 查询成功后输出账号和密码，失败后输出失败
                PassWord passWord = PassWordToll.findPassWord(lookPassWord, linkedList);
                if(passWord != null){
                    System.out.println("账号是：" + passWord.getUser() + "\n" +
                            "密码是：" + passWord.getPassWord() + "\n");
                }else {
                    System.out.println("没有找到你所查询的密码！\n");
                }

            }else if ("2".equals(sign)) {
                // 添加密码 ok
                PassWord newPassWord = new PassWord();

                System.out.print("请输入密码备注：");
                newPassWord.setName(s.next());
                System.out.print("请输入账号：");
                newPassWord.setUser(s.next());
                System.out.print("请输入密码：");
                newPassWord.setPassWord(s.next());

                // 添加到集合中
                linkedList.add(newPassWord);


                // 把集合写出到本地
                if(PassWordToll.writePassWord("user\\passWord.jpg", linkedList)){
                    System.out.println("添加成功！\n");
                }else {
                    System.out.println("添加失败！\n");
                }

                // 重写加载密码集合
                linkedList = PassWordToll.passWordObjectLoad("user\\passWord.jpg", linkedList);

            }else if ("3".equals(sign)) {
                // 删除密码 ok
                System.out.print("输入你要删除的密码：");
                String delPassWord = s.next();

                // 删除成功后输出删除成功，失败后输出失败
                if(PassWordToll.delPassWord(delPassWord, linkedList)){
                    // 把删除后的集合写出到本地
                    PassWordToll.writePassWord("user\\passWord.jpg", linkedList);
                    // 重写加载新的密码集合
                    linkedList = PassWordToll.passWordObjectLoad("user\\passWord.jpg", linkedList);
                    System.out.println("删除成功！\n");
                }else {
                    System.out.println("没有找到你输入的密码，删除失败！\n");
                }

            }else if ("4".equals(sign)) {
                // 查看已存入密码 ok
                int i = 0;
                for (PassWord passWord : linkedList){
                    i++;
                    // 每3个换行
                    if(i == 4){
                        System.out.print("\n");
                    }
                    System.out.print(passWord.getName() + "\t");
                }
                System.out.print("\n");

            }else if("5".equals(sign)){
                // 修改登录密码
                System.out.print("请输入新的登录密码：");
                String newPassWord = s.next();
                if(PassWordToll.setPassWord("user\\pass\\pass.mm",newPassWord)){
                    System.out.println("修改成功！\n");
                }else {
                    System.out.println("修改失败！\n");
                }

            }else if("6".equals(sign)){
                // 退出程序
                System.exit(0);

            }else {
                System.out.println("你输入的有误，请重新输入！\n");
            }
        }
    }

}
